package im.yisme.view.activities;

import im.yisme.R;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ImportMusicActivity extends Activity {

	private Stack<File> stack;
	private File currentDir;
	private SimpleAdapter adapter;
	private List<Map<String, Object>> list;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		stack = new Stack<File>();
		list = new ArrayList<Map<String,Object>>();
		currentDir = Environment.getExternalStorageDirectory();
		refreshData();
		
		adapter = new SimpleAdapter(this, list, R.layout.activity_import_listitem, new String[]{"img_pre", "text", "img"},   
                new int[]{R.id.img_pre, R.id.text, R.id.img});
		
		ListView listView = new ListView(this);
		listView.setAdapter(adapter);
		setContentView(listView);
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub

				stack.push(currentDir);
				Map<String, Object> item = list.get((int)arg3);
				String itemName = (String)item.get("text");
				currentDir = new File(currentDir, itemName);
				
				if (currentDir.isDirectory()){
					changeData();
				}
				else {
					Intent intent = new Intent();
					intent.putExtra("file", currentDir.getAbsolutePath());
					ImportMusicActivity.this.setResult(RESULT_OK, intent);
					finish();
				}
			}
		});
	}
	
	private void changeData(){
		refreshData();
		adapter.notifyDataSetChanged();
	}
	
	private void refreshData() {
		// TODO Auto-generated method stub
		list.clear();
		String[] items = currentDir.list();
		Arrays.sort(items);
		
        for(int i = 0; i < items.length; i++) { 
        	File file = new File(currentDir, items[i]);
        	String name = file.getName().toLowerCase();
        	if (file.isFile() && !name.endsWith(".mp3")){
        		continue;
        	}      	
        	
            Map<String, Object> map = new HashMap<String, Object>();  
            map.put("text", items[i]);  
            map.put("img", R.drawable.pick);  
            
            int pre = file.isDirectory() ? R.drawable.dir : R.drawable.file;
            map.put("img_pre", pre);           
            list.add(map);  
        }
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			if (!stack.isEmpty()){
				currentDir = stack.pop();
				changeData();
				return true;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
}